# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
import os
import cv2
import pro_canny

def index(request):
    return HttpResponse(u"首页")

def add(request):
    # render(request,'postpic.html')
    # print request.META['CSRF_COOKIE']
    return render(request, 'postpic.html')

# def postpic(request):
#     try:
#         if request.method == "POST" and '127.0.0.1' == request.META['REMOTE_ADDR']:
#            image = request.FILES.get('image')
#            default_storage.save(os.getcwd()+'/images/'+image.name, ContentFile(image.read()))
#            circle_json = pro_canny.crop_face(os.getcwd()+'/images/'+image.name)
#            os.remove(os.getcwd()+'/images/'+image.name)
#            return HttpResponse(circle_json)
#     except:
#         return HttpResponse(u"上传失败！")
def postpic(request):
    try:
        if request.method == "POST" and '127.0.0.1' == request.META['REMOTE_ADDR']:
           print 'post success...'
           image = request.FILES.get('image')
           default_storage.save(os.getcwd()+'/images/'+image.name, ContentFile(image.read()))

           circle_json = pro_canny.crop_face(os.getcwd()+'/images/'+image.name)
           os.remove(os.getcwd()+'/images/'+image.name)
           # print circle_json
           return HttpResponse(circle_json)
    except BaseException,e:
        print 'post exception...'
        return HttpResponse(e)




